home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / strings.zip / STRINGS.C < prev    next >
Text File  |  1988-02-12  |  4KB  |  131 lines

  1. /*
  2.  *      S T R I N G S . C
  3.  *
  4.  *      This program is used to display strings in binary files
  5.  *
  6.  *    Steve Sampson (75136,626), Public Domain (p) November 1987
  7.  *
  8.  *     Updated 12/29/87 - Pete Lyall (76703,4230)
  9.  *
  10.  *    - bug fix: in_string was previously never reset to FALSE,
  11.  *      resulting in erroneous marking of string start.
  12.  *
  13.  *    - hex offset of each string start is now provided to ease
  14.  *      hacking/patching of files.
  15.  *
  16.  *    Define MSDOS for that type machine, defaults to OS9
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21.  
  22. #ifdef MSDOS
  23. #include <fcntl.h>
  24. #define    READ    O_RDONLY
  25. #else
  26. #include <lowio.h>
  27. #endif
  28.  
  29. #define FALSE   0
  30. #define TRUE    ~FALSE
  31. #define DEFCHARS 3
  32. #define MAXLINE 256
  33.  
  34.  
  35. main(argc, argv)
  36. int     argc;
  37. char    *argv[];
  38. {
  39.      int     i, ercode, lptr, nchars = DEFCHARS, fd, in_string;
  40.      char    *fname, inbuff[MAXLINE], outbuff[MAXLINE];
  41. #ifndef MSDOS
  42.      long    sofar = 0L, sstart;
  43.  
  44.      pflinit();
  45. #endif
  46.  
  47.      if (argc < 2 || argc > 3)  
  48.      {
  49.           fprintf(stderr, "Usage: strings [n] <filename>\n");
  50.           fprintf(stderr, "Where n is the minimum width (default is 3)\n");
  51.           exit(0);
  52.      }
  53.  
  54.      if (argc == 3)  
  55.      {
  56.           nchars = abs(atoi(argv[1]));
  57.           if (nchars < 1 || nchars > MAXLINE - 1)  
  58.           {
  59.                fprintf(stderr, "strings: Maximum width is %d\n", MAXLINE - 1);
  60.                exit(1);
  61.           }
  62.  
  63.           fname  = argv[2];
  64.      }
  65.      else
  66.           fname  = argv[1];
  67.  
  68.  
  69.      if ((fd = open(fname, READ)) == -1)  
  70.      {
  71.           fprintf(stderr, "strings: File `%s' Not Found\n", fname);
  72.           exit(1);
  73.      }
  74.  
  75.      lptr = 0;                               /* Index into outbuff        */
  76.      in_string = FALSE;                      /* Not in a string yet       */
  77.  
  78.      while ((ercode = read(fd, inbuff, MAXLINE)) > 0)  
  79.      {
  80.           for (i = 0; i < ercode; i++)  
  81.           {
  82.  
  83.                /* Search entire buffer */
  84.  
  85.                if (in_string && isprint(inbuff[i]))
  86.  
  87.                     /* We are in a string and are continuing */
  88.  
  89.                     outbuff[lptr++] = inbuff[i];
  90.  
  91.                else 
  92.                     if (isprint(inbuff[i]))   /* would this char start one ? */
  93.                     {
  94.                          outbuff[lptr++] = inbuff[i]; /* start new one */
  95.                          in_string = TRUE;      /* We're in a string now */
  96. #ifndef MSDOS
  97.                          sstart = i+sofar;      /* mark string start */
  98. #endif
  99.                     }
  100.                     else 
  101.                          if (in_string) /* if in one, would this end it? */
  102.                          {
  103.                               /* End of string */
  104.                               if (lptr >= nchars)  /* enough to tell about? */
  105.                               {
  106.                                    /* Found a long-enough string */
  107.                                    outbuff[lptr] = '\0';
  108.  
  109.                                    /* Terminate the string and print */
  110.  
  111. #ifdef MSDOS
  112.                    printf("%s\n", outbuff);
  113. #else
  114.                                    printf("%6lx: %s\n", sstart, outbuff);
  115. #endif
  116.                               }
  117.  
  118.                               in_string = FALSE; /* PWL */
  119.                               lptr = 0;       /* Start at beginning again */
  120.                          }
  121.  
  122.           }
  123.  
  124. #ifndef MSDOS
  125.           sofar += (long)ercode;   /* update how far we've read into buffers */
  126. #endif
  127.      }
  128.  
  129.      close(fd);
  130. }
  131.